package org.sippr.service.impl;

import org.sippr.mapper.SdCheckPointsMapper;
import org.sippr.pojo.SdCheckPoints;
import org.sippr.service.SdCheckPointsService;
import org.sippr.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class SdCheckPointsImpl implements SdCheckPointsService {

    @Autowired
    private SdCheckPointsMapper sdCheckPointsMapper;

    @Override
    public void add(SdCheckPoints value) {
        //获取登陆人id
        Map<String, Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");

        value.setUpdateUser(id);
        value.setCreateUser(id);

        //记录时间
        LocalDateTime time = LocalDateTime.now();
        value.setCreateTime(time);
        value.setUpdateTime(time);

        if (value.getWordCue().isEmpty()) {
            value.setWordCue(",");
        }
        if (value.getWordNega().isEmpty()) {
            value.setWordNega(",");
        }
        sdCheckPointsMapper.insert(value);
    }

    @Override
    public List<SdCheckPoints> list() {
        return sdCheckPointsMapper.list();
    }

    @Override
    public SdCheckPoints findByName(String name) {
        return sdCheckPointsMapper.findByName(name);
    }

    @Override
    public SdCheckPoints findByAlias(String alias) {
        return sdCheckPointsMapper.findByAlias(alias);
    }

    @Override
    public void update(SdCheckPoints value) {

        Map<String, Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");

        value.setUpdateUser(id);

        //记录时间
        LocalDateTime time = LocalDateTime.now();
        value.setUpdateTime(time);

        sdCheckPointsMapper.updateByPrimaryKey(value);
    }

    @Override
    public void delete(Integer id) {
        sdCheckPointsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SdCheckPoints get(Integer cpId) {
        return sdCheckPointsMapper.selectByPrimaryKey(cpId);
    }
}
